package com.tony.server.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tony.easymvc.mapper.BizDbMapper;
import com.tony.easymvc.mapper.DbInfoMapper;
import com.tony.easymvc.mapper.ProjectBizMapper;
import com.tony.easymvc.po.BizDb;
import com.tony.easymvc.po.BizDbExample;
import com.tony.easymvc.po.DbInfo;
import com.tony.easymvc.po.ProjectBiz;
import com.tony.easymvc.po.ProjectBizExample;

@Controller
@RequestMapping("biz")
public class ProjectBizController {

    @Resource
    ProjectBizMapper projectBizMapper;

    @Resource
    BizDbMapper      bizDbMapper;
    @Resource
    DbInfoMapper dbInfoMapper;

    @RequestMapping("add")
    @ResponseBody
    public void save(HttpServletRequest request) {
        String projectId = request.getParameter("id");
        String bizName = request.getParameter("bizName");
        String mdb = request.getParameter("masterDb");
        String sdb = request.getParameter("slaverDb");
        ProjectBiz pb = new ProjectBiz();
        pb.setProjectId(Integer.parseInt(projectId));
        pb.setBizName(bizName);
        projectBizMapper.insertSelective(pb);

        int bizId = pb.getId();
        BizDb bizDbm = new BizDb();
        bizDbm.setBizId(bizId);
        bizDbm.setDbId(Integer.parseInt(mdb));
        bizDbm.setIsMaster(1);
        bizDbMapper.insertSelective(bizDbm);

        BizDb bizDbs = new BizDb();
        bizDbs.setBizId(bizId);
        bizDbs.setDbId(Integer.parseInt(sdb));
        bizDbs.setIsMaster(0);
        bizDbMapper.insertSelective(bizDbs);
    }

    @RequestMapping("list")
    @ResponseBody
    public String list(int id) {
        ProjectBizExample example = new ProjectBizExample();
        example.or().andProjectIdEqualTo(id);
        List<ProjectBiz> items = projectBizMapper.selectByExample(example);
        if (items == null || items.isEmpty()) {
            return "[]";
        }
        JSONArray arr = new JSONArray();
        for (int i = 0; i < items.size(); i++) {
            ProjectBiz temp = items.get(i);
            BizDbExample example1 = new BizDbExample();
            example1.or().andBizIdEqualTo(temp.getId());
            JSONObject o = new JSONObject();
            o.put("id", temp.getId());
            o.put("bizName", temp.getBizName());


            List<BizDb> bds = bizDbMapper.selectByExample(example1);
            for (int j = 0; j < bds.size(); j++) {
                BizDb bizDb = bds.get(j);
                if(bizDb.getIsMaster() == 1) {
                    DbInfo dbinfo = dbInfoMapper.selectByPrimaryKey( bizDb.getDbId());
                    o.put("url", dbinfo.getJdbcUrl());
                    o.put("username", dbinfo.getUsername());
                    o.put("password", dbinfo.getPassword());
                    break;
                }
            }
            arr.add(o);
        }
        return arr.toJSONString();
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(int id) {
        projectBizMapper.deleteByPrimaryKey(id);
        return "操作成功";
    }
    
}
